package com.coocaa.x.demo.servlets.parser;

import com.coocaa.x.demo.servlets.data.AppCategoryData;
import com.coocaa.x.demo.servlets.data.AppDetailData;
import com.coocaa.x.demo.servlets.data.AppListData;
import com.coocaa.x.demo.servlets.data.ParserData.AppCategory;
import com.coocaa.x.demo.servlets.data.ParserData.AppCategory.AppCategoryItem;
import com.coocaa.x.demo.servlets.data.ParserData.AppDetailObject;
import com.coocaa.x.demo.servlets.data.ParserData.AppDetailObject.AppDetailItem;
import com.coocaa.x.demo.servlets.data.ParserData.AppListObject;
import com.coocaa.x.demo.servlets.data.ParserData.AppListObject.AppListItem;
import com.coocaa.x.demo.servlets.data.ParserData.CategoryObject;
import com.coocaa.x.demo.servlets.data.ParserData.CategoryObject.CategoryItem;
import com.coocaa.x.demo.servlets.data.ParserData.SubCategoryObject;
import com.coocaa.x.demo.servlets.data.ParserData.SubCategoryObject.SubCategoryItem;
import com.coocaa.x.demo.servlets.data.ParserData.SubCategoryObject.SubCategoryItem.FiltersItem;
import com.coocaa.x.demo.servlets.data.ParserData.VideoDetailObject;
import com.coocaa.x.demo.servlets.data.ParserData.VideoDetailObject.VideoDetail;
import com.coocaa.x.demo.servlets.data.ParserData.VideoListObject;
import com.coocaa.x.demo.servlets.data.ParserData.VideoListObject.VideoItem;
import com.coocaa.x.demo.servlets.data.ParserData.VideoSearch;
import com.coocaa.x.demo.servlets.data.ParserData.VideoSearch.VideoSearchList;
import com.coocaa.x.demo.servlets.data.ParserData.VideoSearch.VideoSearchList.FromItem;
import com.coocaa.x.framework.utils.LogUtils;

import org.apache.http4.HttpResponse;
import org.apache.http4.HttpStatus;
import org.apache.http4.client.HttpClient;
import org.apache.http4.client.methods.HttpGet;
import org.apache.http4.impl.client.DefaultHttpClient;
import org.apache.http4.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by admin on 2015/9/9.
 */
public class JsonParser {

    public static void main(String[] args) {
//        testCategory();
//        testSubCategory();
//        testVideoList();
//        testVideoDetail();
//        testVideoSearch();
//        testAppCategory();
//        testAppList();
//        testAppDetail();
    }

    private static void testCategory() {
        CategoryObject object = CategoryObject.parseJObject("", CategoryObject.class);
        System.out.println(object.data);
//        CategoryList mCategoryList = CategoryList.parseJObject(object.data,CategoryList.class);
        List<CategoryItem> list = object.data;
        for (CategoryItem item : list)
            System.out.println(item.categoryId + " " + item.categoryName + " " + item.categoryThumb);
    }

    private static void testSubCategory() {
        SubCategoryObject subCategoryObject = SubCategoryObject.parseJObject("", SubCategoryObject.class);
        System.out.println(subCategoryObject.data);
        List<SubCategoryItem> list = subCategoryObject.data;
        for (SubCategoryItem subItem : list) {
            System.out.println(subItem.subcategoryName + " " + subItem.subcategoryType + " " + subItem.subcategoryFilters);
            List<FiltersItem> list2 = subItem.subcategoryFilters;
            for (FiltersItem item : list2) {
                System.out.println(item.filterName + " " + item.filterValue);
            }
        }
    }

    private static void testVideoList() {
        VideoListObject vo = VideoListObject.parseJObject("", VideoListObject.class);
        System.out.println(vo.data);
        List<VideoItem> list = vo.data;
        for (VideoItem item : list)
            System.out.println(item.isFree + " " + item.videoName + " " + item.categoryId);
    }

    private static void testVideoDetail() {
        VideoDetailObject vdo = VideoDetailObject.parseJObject("", VideoDetailObject.class);
        VideoDetail vd = vdo.data;
        System.out.println(vd.currentEpisode + " " + vd.videoName + " " + vd.categoryId);
    }

    private static void testVideoSearch() {
        VideoSearch vs = VideoSearch.parseJObject("", VideoSearch.class);
        List<VideoSearchList> list = vs.data;
        for (VideoSearchList item : list) {
            System.out.println(item.videoName + " " + item.categoryName + " " + item.fromList);
            for (FromItem item2 : item.fromList) {
                System.out.println(item2.firstUrl + " " + item2.fromName + " " + item2.fromSite);
            }
        }
    }

    private static List<AppCategoryData> getAppCategory(String json) {
        AppCategory ac = AppCategory.parseJObject(json, AppCategory.class);
        List<AppCategoryItem> list = ac.data;
        List<AppCategoryData> categoryList = new ArrayList<AppCategoryData>();
        for (AppCategoryItem item : list) {
            System.out.println(item.sortName + " " + item.updateTime + " " + item.subSorts);
            AppCategoryData data = new AppCategoryData();
            data.sortId = item.sortId;
            data.sortName = item.sortName;
            data.appCount = item.appCount;
            categoryList.add(data);
            //二级分类暂时不补充了
//            List<SubSortItem> list2 = item.subSorts;
//            for (SubSortItem item2 : list2)
//                System.out.println(item2.sortName + " " + item2.appCount);
        }
        return categoryList;
    }

    private static List<AppListData> getAppList(String json){
        AppListObject alo = AppListObject.parseJObject(json,AppListObject.class);
        int count = alo.count;
        List<AppListItem> list = alo.data;
        List<AppListData> appList = new ArrayList<AppListData>();
        for(AppListItem item : list)
        {
            System.out.println(item.name+" "+item.packagename);
            AppListData data = new AppListData();
            data.id = item.id;
            data.name = item.name;
            data.packagename = item.packagename;
            data.downloads = item.downloads;
            data.lmg = item.lmg;
            data.version = item.version;
            data.apk = item.apk;
            appList.add(data);
        }
        return appList;
    }

    public static AppDetailData getAppDetail(String json){
        AppDetailObject ado = AppDetailObject.parseJObject(json,AppDetailObject.class);
        AppDetailItem item = ado.details;
        System.out.println(item.packageName+" "+item.title+" "+item.id);
//        String[] list = item.screenShotsPreview;
//        for(String preview:list)
//            System.out.println(preview);

        AppDetailData data = new AppDetailData();
        data.packageName = item.packageName;
        data.id = item.id;
        data.title = item.title;
        data.iconUrl = item.iconUrl;
        data.screenShots = item.screenShots;
        return data;
    }

//    public static String getJsonFromWeb(String url){
//        LogUtils.d("webdata","getJsonFromWeb  url:"+url);
//        StringBuilder sb  = "";
//        try {
//            String inputLine = "";
//            URL uri = new URL(url);
//            BufferedReader in = new BufferedReader(
//                    new InputStreamReader(
//                            uri.openStream()));
//            while ((inputLine = in.readLine()) != null)
//                sb.append(inputLine + "\n");
//            in.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        LogUtils.d("webdata","getJsonFromWeb  sb:"+sb);
//        return sb;
//    }

    public static String getJsonFromWeb (String httpUrl) {
        LogUtils.d("webdata", "getJsonFromWeb  httpUrl:" + httpUrl);
        String strResult = "";
        // HttpGet连接对象
        HttpGet httpRequest = new HttpGet(httpUrl);
        try{
            // 取得HttpClient对象
//            HttpClient httpclient =  HttpConnectionManager.getHttpClient();
            HttpClient httpclient =  new DefaultHttpClient();
            // 请求HttpClient，取得HttpResponse
            HttpResponse httpResponse = httpclient.execute(httpRequest);

            int statCode = httpResponse.getStatusLine().getStatusCode();
            // 请求成功
            if (statCode == HttpStatus.SC_OK) {
                // 取得返回的字符串
                strResult = EntityUtils.toString(httpResponse.getEntity());
            } else {
                LogUtils.d("webdata","HttpClient请求"+httpUrl+"出错，错误代码:" + statCode);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            //释放http连接
            httpRequest.releaseConnection();
        }
        LogUtils.d("webdata","getJsonFromWeb  strResult:"+strResult);
        return strResult;
    }
}
